import os
import sys
# program template
source = """

%s

public class IJava{
	public static void main(String[] args){
	%s		
}
}
"""

class IJava:
	
	def __init__(self):
		self.import_stack = ["import java.io.*",'']
		self.code_stack = []

		print "starting...."

	def _check_java_(self):
		try:
			os.system('java -version')
		except:
			print "java could not be found. please make sure that you've installed java and it is in your system path."

	def load_shell(self):
		self._check_java_()
		print "java loaded"
		while(True):
			try:
				sys.stdout.write(">> ")
				ri = raw_input()
				self.code_stack.append(ri)
				self._write_and_execute()
				self.code_stack = []
			except KeyboardInterrupt:
				sys.stdout.write(" Keyboard interrupt!");
				self._clean_exit()
		
	def _write_and_execute(self):
		prg = self._load_program()
		f = open("IJava.java", 'w')
		f.write(prg)
		f.close()
		os.system("javac IJava.java")
		os.system("java IJava") 
	def _load_program(self):
		program = source %(';'.join(self.import_stack), ';'.join(self.code_stack))
		return program

	def _clean_exit(self):
		sys.stdout.write("\n");
		sys.exit(1)	

if __name__ == '__main__':
	ij = IJava()
	ij.load_shell()
		
